home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3887 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: sdrc.com!thor!scjones
  2. From: scjones@thor.sdrc.com (Larry Jones)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with arrays
  5. Date: 31 Jan 1996 18:37:12 GMT
  6. Organization: SDRC Engineering Services
  7. Distribution: world
  8. Message-ID: <4eocso$a98@info1.sdrc.com>
  9. References: <4ejtia$6id@sifon.cc.mcgill.ca> <4en2ig$6af@gryphon.phoenix.net>
  10. NNTP-Posting-Host: thor.sdrc.com
  11.  
  12. In article <4en2ig$6af@gryphon.phoenix.net>, brucew@phoenix.net (Bruce Wedding) writes:
  13. > C does not explicitly initialize arrays unless they are defined
  14. > global.  You will have to zero out the array.  You can use a loop
  15. > or the memset function.  I think the loop is a little safer,
  16. > though maybe not as fast.
  17.  
  18. While C doesn't initialize auto arrays, it allows you to do so.  So the
  19. best thing to do is just:
  20.  
  21.     int letters[100] = {0};
  22.  
  23. (If there are fewer initializers than array elements, the rest of the
  24. elements are initialize to 0, which is just what we want in the case. 
  25. If you wanted to initialize it to something other than 0, you'd have to
  26. specify all 100 initializers.)
  27.  
  28. As for using a loop versus memset, the loop is more flexible since you
  29. can initialize to any value at all, but for initializing to 0 both are
  30. equally safe since an integer 0 is required to be all bits zero (this
  31. is NOT true for floating point zeros or null pointers, however).
  32. ----
  33. Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
  34. larry.jones@sdrc.com
  35. Oh, now don't YOU start on me. -- Calvin
  36.